<template>
  <el-form :model="nodeModalForm" ref="nodeModal"
           label-placement="left"
           label-width="100px"
           :rules="nodeModalRules"
           size="small">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model="nodeModalForm.nodeName"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="去重字段" prop="distinct">
          <el-select v-model="nodeModalForm.distinct" clearable>
            <el-option v-for="item in fieldsList"
                       :key="item.value"
                       :label="item.label"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <div class="nodeModal-footer">
      <span class="dialog-footer">
        <el-button plain @click="handleConfigCancel">取消</el-button>
        <el-button type="primary" @click="handleConfigConfirm">更新</el-button>
      </span>
    </div>
  </el-form>
</template>

<script>
import { computed, defineComponent, inject, onMounted, ref } from "vue";
import { useStore } from "vuex";

export default defineComponent({
  name: "node-config-distinct",
  props: {
    node: Object,
  },
  components: {},
  setup(props, ctx) {
    const store = useStore();
    const flowData = computed(() => store.state.flowEtl.flowData);
    const nodeList = computed(() => flowData.value.nodeList);
    const nodeConfigClose = inject("nodeConfigClose");
    const fieldsList = ref([]);
    const nodeModalForm = ref({
      nodeName: "",
      distinct: [],
    });
    const nodeModal = ref();
    const tableLoading = ref(false);
    const nodeModalRules = ref({
      nodeName: {
        required: true,
        message: "请输入数据源名称",
        trigger: "blur",
      },
      distinct: {
        required: true,
        message: "请选择要去重的字段",
        trigger: "change",
      }
    });
    const handleConfigCancel = () => {
      nodeConfigClose();
    };
    const nodeConfigUpdate = (node) => {
      let nodes = [...nodeList.value].map(item => {
        if (item.id === node.id) {
          item.status = true;
          item.nodeName = node.nodeName;
          item.distinct = node.distinct;
        }
        return item;
      });
      store.commit("flowEtl/set_nodeList", nodes);
      nodeConfigClose();
    };
    const handleConfigConfirm = () => {
      nodeModal.value.validate((valid) => {
        if (valid) {
          nodeConfigUpdate(nodeModalForm.value);
        } else {
          return false;
        }
      });
    };
    const resetForm = () => {
      nodeModalForm.value = {
        id: null,
        nodeName: "",
        distinct: []
      };
    };
    onMounted(() => {
      resetForm();
      nodeModalForm.value = {
        id: props.node.id,
        nodeName: props.node.nodeName,
        distinct: props.node.config.distinct,
      };
    });
    return {
      nodeList,
      tableLoading,
      nodeModalForm,
      fieldsList,
      nodeModalRules,
      nodeModal,
      resetForm,
      handleConfigCancel,
      handleConfigConfirm,
      nodeConfigUpdate,
      nodeConfigClose,
    };
  },
});
</script>

<style scoped>
.field-table {
  padding: 0 12px 24px;
}

.nodeModal-footer {
  padding: var(--el-dialog-padding-primary);
  padding-top: 10px;
  text-align: right;
  box-sizing: border-box;
}
</style>